package com.king.java.tools.ssh;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述: ssh工具的通用处理
 *
 * @author : sujinchen
 * @date : 2020/10/21
 */
public abstract class AbstractSSHutils {
    /**
     * 存放ssh连接信息的mao
     */
    private static Map<String, Object> sshMap = new ConcurrentHashMap<>();

    private Logger log = LoggerFactory.getLogger(AbstractSSHutils.class);

    /**
     * 获取ssh连接
     * @param key
     * @return
     */
    public Object getSSH(String key) {
        return sshMap.get(key);
    }

    /**
     * 存储ssh连接
     * @param key
     * @param ssh
     */
    public void setSSH(String key, Object ssh) {
        sshMap.put(key, ssh);
    }

    public String getKey(String flag) {

         String key=System.currentTimeMillis() + flag + (int)(Math.random() * 10000);
        System.out.println(key+"登陆成功！");
        return key;
    }
    /**
     * 移除ssh连接
     * @param key
     */
    public void removeSSH(String key) {
        sshMap.remove(key);
    }


    /**
     * 文本读取
     * @param resultLines
     * @param in
     * @throws IOException
     */
    public void bufferRead(List<String>resultLines,InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String inputLine = null;
        while ((inputLine = reader.readLine()) != null) {
            System.out.println(inputLine);
            log.info(inputLine);
            resultLines.add(inputLine);
        }
    }



}
